﻿// ______________________________________________________________
//
// Debris animation class - Liam O'Donnell
// Makes a movieclip fall randomly, like debris from an explosion
// ______________________________________________________________
//

// ______________________________________________________________
//
// USAGE:
// - Create/attach a movieclip that's an instance of Debris
// - You can alternatively use the static method 'create'
// ______________________________________________________________
//

import com.spikything.utils.SpikyTools;

class com.spikything.anim.Debris extends MovieClip {

	// ______________________________________________________
	//
	// CONSTANTS
	// ______________________________________________________
	//

	private var friction 	:Number;
	private var vx 			:Number;
	private var vy 			:Number;
	private var vr 			:Number;
	private var g 			:Number;

	// ______________________________________________________
	//
	// CONSTRUCTOR
	// ______________________________________________________
	//

	function Debris () {}

	// ______________________________________________________
	//
	// PUBLIC METHODS
	// ______________________________________________________
	//

	public function onLoad ():Void {
		init();
	}

	// ______________________________________________________
	//
	// PRIVATE METHODS
	// ______________________________________________________
	//

	private function init ():Void {
		gotoAndStop(random(_totalframes)+1);
		friction = friction==undefined ? .92 : friction;
		g = g==undefined ? 1 : g;
		vx = random(25)-12;
		vy = random(25)-12;
		vr = random(25)-12;
		onEnterFrame = update;
	}

	private function update ():Void {
		if (!random(9)) gotoAndStop(random(_totalframes)+1);
		vy += g;
		vx *= friction;
		vy *= friction;
		_x += vx;
		_y += vy;
		_rotation += vr;
		if (_y>100 && vy>0 && !random(30)) vy *= -.8;
		if (_y>Stage.height) this.removeMovieClip();
	}

	// ______________________________________________________
	//
	// STATIC METHODS
	// ______________________________________________________
	//

	public static function create (_base:MovieClip,_id:String,_initObj:Object,_depth:Number):Debris {
		return SpikyTools.attachClass(_base,_id,Debris,_initObj,null,_depth);
	}

}